package com.t2two.example.common.dataresult;



import java.io.Serializable;
import java.util.function.Supplier;

/**
 * @author 李飞
 * @date 2022/1/13
 * @desc
 */
public class BaseResult implements Serializable {

    private static final long serialVersionUID = -388732076184681260L;

    private String traceId;

    private boolean success;

    private int code;

    private String message;

    private String errorCode;

    public BaseResult() {
        this.success = true;
        this.code = ResultCode.SUCCESS.getCode();
        this.message = ResultCode.SUCCESS.getMessage();
    }

    public BaseResult(BaseResult result) {
        this.success = result.isSuccess();
        this.code = result.getCode();
        this.message = result.getMessage();
    }

    /**
     * init a fail result by copy WdkBaseResult
     */
    public <R extends BaseResult> R initFailResult(R result, ResultCode failCode, Object... args) {
        String message = failCode.getMessage();
        if (args != null && args.length > 0) {
            message = String.format(message, args);
        }
        result.setSuccess(false);
        result.setCode(failCode.getCode());
        result.setMessage(message);
        return result;
    }

    /**
     * init a fail result by copy WdkBaseResult
     */
    public <R extends BaseResult> R initFailResult(Supplier<R> supplier, ResultCode failCode, Object... args) {
        return initFailResult(supplier.get(), failCode, args);
    }

    /**
     * init a fail result by copy WdkBaseResult
     */

    public <R extends BaseResult> R initFailResult(Supplier<R> supplier, ResultCode failCode) {
        return initFailResult(supplier.get(), failCode);
    }

    public <R extends BaseResult> R initFailResult(R result, ResultCode failCode) {
        result.setSuccess(false);
        result.setCode(failCode.getCode());
        result.setMessage(failCode.getMessage());
        return result;
    }

    public BaseResult initFailResult(ResultCode failCode) {
        return initFailResult(this, failCode);
    }

    public BaseResult initFailResult(ResultCode failCode, Object... args) {
        return initFailResult(this, failCode, args);
    }

    public static BaseResult success() {
        return new BaseResult();
    }


    public static BaseResult fail(ResultCode failCode, Object... args) {
        BaseResult result = new BaseResult();
        result.initFailResult(failCode, args);
        return result;
    }

    public static BaseResult failure(String message) {
        BaseResult result = new BaseResult();
        result.setMessage(message);
        result.setSuccess(false);
        return result;
    }

    public static BaseResult failure(String message, ResultCode code) {
        BaseResult result = new BaseResult();
        result.setCode(code.getCode());
        result.setMessage(message);
        result.setSuccess(false);
        return result;
    }

    public String getTraceId() {
        return traceId;
    }

    public void setTraceId(String traceId) {
        this.traceId = traceId;
    }

    public boolean isSuccess() {
        return success;
    }

    public boolean isFail() {
        return !success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    @Override
    public String toString() {
        return "WdkBaseResult{" +
                "traceId='" + traceId + '\'' +
                ", success=" + success +
                ", code=" + code +
                ", message='" + message + '\'' +
                '}';
    }
}
